URL = "ftp://ftp.hdfgroup.org/HDF/HDF_Current/src/hdf-%(VERSION)s.tar.bz2"
DEPENDS = ["zlib", "jpeg"]
VERSIONS = ["4.2.5"]
DEFAULT_VERSION = "4.2.5"

from source.builders import ConfigureMakeBuilder, VisualStudioBuilder
import subprocess, os

class BuildHDF4( ConfigureMakeBuilder ):
   
  def configureparameters(self):
    return["--disable-fortran", "--enable-shared"]

class BuildHDF4VisualStudio( VisualStudioBuilder ):

  def configure( self ):
    # run copy_hdf.bat
    self.windir = self.extractedpath + "/windows"
    os.chdir( self.windir )
    subprocess.check_call(["copy_hdf.bat"])

    # Create a new environment
    self.hdf4env = os.environ
    self.hdf4env['HDF4_EXT_ZLIB'] = "zlib.lib" 
    self.hdf4env['HDF4_EXT_JPEG'] = "libjpeg.lib"

    # Set the INCLUDE and LIB
    includes = [self.dependencies["zlib"].getincludedir(), self.dependencies["jpeg"].getincludedir()]
    self.hdf4env["INCLUDE"] = self.hdf4env["INCLUDE"]+";".join(includes)

    lib = [self.dependencies["zlib"].getlibdir(), self.dependencies["jpeg"].getlibdir() ]
    self.hdf4env["LIB"] = self.hdf4env["LIB"]+";".join(lib)

    # Remove SZIP
    replacement = [( ".*H4_HAVE_LIBSZ.*", "" ), (".*HAVE_SZIP_ENCODER.*", ""), 
        (".*H4_HAVE_SZLIB_H.*", "")]
    self.replaceLinesInFile(self.extractedpath + "/hdf/src/h4config.h", replacement )

  def make(self):
    return
    if self.machine.isVS2008():
      os.chdir( self.windir + "/proj/all" )
    else:
      os.chdir( self.windir + "/vs8/all" )
    # Some tests and demos cannot be build. We only need the dll's. 

    platform = "|Win32"
    if self.machine.bits == 64:
      platform = "|x64"

    #only build needed libs
    for (proj, config) in [("hdf", "Release"), ("hdf", "Debug"), ("hdf_dll", "Release"),("hdf_dll", "Debug"),
                           ("mfhdf", "Release"), ("mfhdf", "Debug"), ("mfhdf_dll", "Release"), ("mfhdf_dll", "Debug") ]:
        print ["devenv.com", "all.sln", "/Project", proj, "/Build", config + platform, "/useenv"]
        subprocess.call(["devenv.com", "all.sln", "/Project", proj, "/Build", config + platform, "/useenv"], env=self.hdf4env )
    
    #subprocess.call(["devenv.com", "all.sln", "/Build", "Release" + platform, "/useenv"], env=self.hdf4env )
    #subprocess.call(["devenv.com", "all.sln", "/Build", "Debug" + platform, "/useenv"], env=self.hdf4env )

  def install(self):
    print "self.targetpath", self.targetpath()
    self.copyfile( "hdf/src/Release/hd425.lib", libdir )
    self.copypattern("hdf/src", "^.*\\.h$", includedir )

    self.copyfile( "mfhdf/libsrc/Debug/hm425md.dll", libdir )
    self.copyfile( "mfhdf/libsrc/Debug/hm425d.lib", libdir )
    self.copyfile( "mfhdf/libsrc/Release/hm425m.dll", libdir )
    self.copyfile( "mfhdf/libsrc/Release/hm425.lib", libdir )
    self.copypattern("mfhdf/libsrc", "^.*\\.h$", includedir )


############

def decider( machine, dirs ):
  return { "Linux" : BuildHDF4, "Windows" : BuildHDF4VisualStudio }[machine.platform]

BUILDER = decider
